package com.wteam.dragon.shop.service.impl;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.dragon.shop.mapper.NewProductHomeMapper;
import com.wteam.dragon.shop.mapper.ProductMapper;
import com.wteam.dragon.shop.pojo.bean.NewProductHome;
import com.wteam.dragon.shop.pojo.bean.Product;
import com.wteam.dragon.shop.service.NewProductHomeService;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 陈晓辉
 * @Date 2021/3/25 20:47
 */
@Service
@RequiredArgsConstructor
public class NewProductHomeServiceImpl implements NewProductHomeService {
    private ProductMapper productMapper;
    private NewProductHomeMapper newProductHomeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage addNewProductHome(List<Long> ids) {
        ids.forEach(id -> {
            Product product = productMapper.selectById(id);
            NewProductHome newProductHome = new NewProductHome();
            newProductHome.setProductId(id);
            newProductHome.setProductName(product.getName());
            newProductHomeMapper.insert(newProductHome);
        });
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("添加成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage delNewProductHome(List<Long> ids) {
        newProductHomeMapper.deleteBatchIds(ids);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("删除成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage editNewProductHome(Long id, Long sort) {
        NewProductHome newProductHome = new NewProductHome();
        newProductHome.setId(id);
        newProductHome.setSort(sort);
        newProductHomeMapper.updateById(newProductHome);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("更改成功");
        return resultMessage;
    }

    @Override
    public ResultMessage pageNewProductHome(Page<NewProductHome> page, String productName, String recommendStatus) {
        QueryWrapper<NewProductHome> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(productName != null, NewProductHome::getProductName, productName)
                .eq(recommendStatus != null, NewProductHome::getRecommendStatus, recommendStatus);
        Page<NewProductHome> newProductHomePage = newProductHomeMapper.selectPage(page, queryWrapper);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("newProductHomePage", newProductHomePage);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage editNewProductHomeStatus(List<Long> ids, Integer status) {
        ids.forEach(id -> {
            NewProductHome newProductHome = new NewProductHome();
            newProductHome.setId(id);
            newProductHome.setRecommendStatus(status);
            newProductHomeMapper.updateById(newProductHome);
        });
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("更改成功");
        return resultMessage;
    }

}
